<?php
/**
 * 验证码
 * User: dxk
 * Date: 2020/07/17
 */
namespace App\Http\Controllers\Service;

use App\Http\Controllers\Controller;
use App\Library\Service\Captcha;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;

class CaptchaController extends Controller
{
    /**
     * 图片验证码
     * @param Request $request
     */
    public function index(Request $request)
    {
        $width = $request->input('width', 150);
        if (!is_numeric($width) || $width <= 0) {
            $width = 150;
        }
        $height = $request->input('height', 40);
        if (!is_numeric($height) || $height <= 0) {
            $height = 40;
        }
        $random_char = '23456789';
        $parse = easy_random(4, $random_char);
        $builder = new CaptchaBuilder($parse);
        $builder->setDistortion(false)
            ->setBackgroundColor(229, 229, 229)
            ->build($width, $height);
        Captcha::storeCaptcha($builder->getPhrase());
        header('Content-type: image/jpeg');
        $builder->output();
    }
}
